//
//  SettingsExampleViewController.m
//  SettingsExample
//

#import "SettingsExampleViewController.h"

@implementation SettingsExampleViewController

- (void)saveClick:(id)sender {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
														 NSUserDomainMask, YES);
	NSString *documentsDir = [paths objectAtIndex:0];
	
	NSString *filePath = [documentsDir stringByAppendingPathComponent:settingsFileName];
	
	[dictSettings setValue:txtUserName.text forKey:@"username"];
	[dictSettings setValue:txtPassword.text forKey:@"password"];
	
	// 파일로 저장
	[dictSettings writeToFile:filePath atomically:NO];
}

- (NSDictionary *)getSettingsDictionary {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
														 NSUserDomainMask, YES);
	NSString *documentsDir = [paths objectAtIndex:0];
	NSString *filePath = [documentsDir stringByAppendingPathComponent:settingsFileName];
	NSFileManager *fm = [NSFileManager defaultManager];
	BOOL exists = [fm fileExistsAtPath:filePath];
	
	if (!exists) {
		NSString *resourcesDir = [[NSBundle mainBundle] resourcePath];
		NSString *sourcePath = [resourcesDir stringByAppendingPathComponent:settingsFileName];
		[fm copyItemAtPath:sourcePath toPath:filePath error:NULL];
	}
	
	return [NSDictionary dictionaryWithContentsOfFile:filePath];
}

- (void)createUI {
	UILabel *lblUserName = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 40)];
	lblUserName.backgroundColor = [UIColor clearColor];
	lblUserName.text = @"Username:";
	[self.view addSubview:lblUserName];
	[lblUserName release];
	
	txtUserName = [[UITextField alloc] initWithFrame:CGRectMake(115, 20, 185, 24)];
	txtUserName.backgroundColor = [UIColor whiteColor];
	txtUserName.text = [dictSettings objectForKey:@"username"];
	[self.view addSubview:txtUserName];
	
	UILabel *lblPassword = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 100, 40)];
	lblPassword.backgroundColor = [UIColor clearColor];
	lblPassword.text = @"Password:";
	[self.view addSubview:lblPassword];
	[lblPassword release];
	
	txtPassword = [[UITextField alloc] initWithFrame:CGRectMake(115, 60, 185, 24)];
	txtPassword.backgroundColor = [UIColor whiteColor];
	txtPassword.text = [dictSettings valueForKey:@"password"];
	txtPassword.clearsOnBeginEditing = YES;
	txtPassword.secureTextEntry = YES;
	[self.view addSubview:txtPassword];
	
	UIButton *btnSave = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	btnSave.frame = CGRectMake(200, 100, 100, 34);
	[btnSave setTitle:@"Save" forState:UIControlStateNormal];
	[btnSave addTarget:self action:@selector(saveClick:)
	  forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:btnSave];
}

- (void)viewDidLoad {
	settingsFileName = [[NSString alloc] initWithString:@"settings.plist"];
	dictSettings = [[NSMutableDictionary alloc] initWithDictionary:[self getSettingsDictionary]];
	
	[self createUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
	[txtUserName release];
	[txtPassword release];
	[settingsFileName release];
	[dictSettings release];
	
    [super dealloc];
}

@end
